package tech.aistar.controller.collecte;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.entity.Collecte;
import tech.aistar.entity.Services;
import tech.aistar.service.collecte.ICollecteService;
import tech.aistar.service.services.IServicesListService;
import tech.aistar.service.services.IServicesService;
import tech.aistar.vo.ServicesAndNameQueryVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2019/9/30 0030.
 */

@Controller
@RequestMapping("/ser")
public class Collectesaddcontroller {
    @Autowired
    private ICollecteService collecteService;
    @Autowired
    private IServicesListService servicesListService;
    @Autowired
    private IServicesService servicesService;

    @RequestMapping("/ser")
    public String select(Model model){
        List<ServicesAndNameQueryVo> list = servicesListService.findServicesAndName();
        model.addAttribute("list",list);
        return "services/serviceDetail";
    }




//     添加收藏

    @RequestMapping("/seradd")
    public String coldel(Model model,  HttpServletRequest request,HttpSession session){

        //获取客户的id
        Integer userId = (Integer) session.getAttribute("userinfoId");
         //获取收藏的id
        String aid =  request.getParameter("id");

        //根据id查询所有
        Services services = servicesService.findById(Integer.valueOf(aid));

        //获取省的name
        String province = servicesService.findById(Integer.valueOf(aid)).getProvince();
        //获取市的name
        String city = servicesService.findById(Integer.valueOf(aid)).getCity();
        //获取图片的路径
        String url = servicesService.findById(Integer.valueOf(aid)).getPath();


        Collecte collecte = new Collecte();
        collecte.setProvince(province);
        collecte.setCity(city);
        collecte.setServicesId(Integer.valueOf(aid));
        collecte.setUrl(url);
        collecte.setUserinfoId(userId);
        collecte.setStatus(0);
        collecteService.colSave(collecte);

        return "redirect:/collection/collection";
    }

    @RequestMapping("/colshou")
    public String col(Model model,  HttpServletRequest request){
        //获取收藏的id
        String aid =  request.getParameter("id");
       Integer dis = collecteService.del(Integer.valueOf(aid));
        model.addAttribute("dis",dis);
        return "redirect:/collection/list";

    }
}
